package dtu.k12tpm.pbs.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.UnknownFormatFlagsException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;
import dtu.k12tpm.pbs.contact.Contact;
import dtu.k12tpm.pbs.util.DateFormat;
import dtu.k12tpm.pbs.util.TimeFormat;

public class MyActivity extends Activity {
	/**
	 * Show toast with duration default
	 * 
	 * @param msg
	 *            string id
	 */
	protected void showToast(int msg) {
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
	}

	protected void showToast(String msg) {
		Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * show message toast with custom duration
	 * 
	 * @param msg
	 * @param duration
	 */
	protected void showToast(int msg, int duration) {
		Toast.makeText(getBaseContext(), msg, duration).show();
	}

	public ArrayList<Contact> createSessionForListView(ArrayList<Contact> contacts){
		
		return contacts;
	}

	/**
	 * Get system date
	 * 
	 * @param format
	 * @return
	 */
	public String getDate(DateFormat format, String separater) {
		if (separater == null) {
			throw new UnknownFormatFlagsException("unsupported for separater '" + separater + "'");
		}
		Calendar c = Calendar.getInstance();
		int mYear;
		int mMonth;
		int mDay;

		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH) + 1;// Months from the calendar are
		// offset by one. Add one if you
		// want human readable.
		mDay = c.get(Calendar.DAY_OF_MONTH);

		if (format == DateFormat.DDMMYYYY) {
			return mDay + separater + mMonth + separater + mYear;
		} else if (format == DateFormat.MMDDYYYY) {
			return mMonth + separater + mDay + separater + mYear;
		} else if (format == DateFormat.YYYYMMDD) {
			return mYear + separater + mMonth + separater + mDay;
		} else {
			return mDay + separater + mMonth + separater + mYear;
		}
	}


	/**
	 * Get system time
	 * 
	 * @return
	 */
	public String getTime() {
		Calendar c = Calendar.getInstance();

		int h;
		int m;
		int s;

		h = c.get(Calendar.HOUR_OF_DAY);
		m = c.get(Calendar.MINUTE);
		s = c.get(Calendar.SECOND);
		return h + ":" + m + ":" + s;
	}

	/**
	 * Get time system. Default 24h
	 * 
	 * @param separater
	 * @return hh:mm:ss
	 */
	public String getTime(String separater) {
		if (separater == null) {
			throw new UnknownFormatFlagsException("unsupported for separater '" + separater + "'");
		}
		Calendar c = Calendar.getInstance();

		int h;
		int m;
		int s;

		h = c.get(Calendar.HOUR_OF_DAY);
		m = c.get(Calendar.MINUTE);
		s = c.get(Calendar.SECOND);
		return h + separater + m + separater + s;
	}


	public ProgressDialog showProgressDialog(String title, String msg, int icon, boolean cancelable) {
		ProgressDialog pd = new ProgressDialog(this);
		if (title != null) {
			pd.setTitle(title);
		}
		if (msg != null)
			pd.setMessage(msg);
		if (icon != -1) {
			pd.setIcon(icon);
		}
		pd.setCancelable(cancelable);
		pd.show();
		return pd;
	}
	
	/**
	 * Show progress dialog with default icon
	 * @param title
	 * @param msg
	 * @param cancelable
	 * @return
	 */
	public ProgressDialog showProgressDialog(String title, String msg, boolean cancelable) {
		ProgressDialog pd = new ProgressDialog(this);
		if (title != null) {
			pd.setTitle(title);
		}
		if (msg != null)
			pd.setMessage(msg);
		pd.setIcon(R.drawable.process_icon);
		pd.setCancelable(cancelable);
		pd.show();
		return pd;
	}
	
	public ProgressDialog showProgressDialog(String msg, boolean cancelable) {
		ProgressDialog pd = new ProgressDialog(this);
		if (msg != null)
			pd.setMessage(msg);
		pd.setIcon(R.drawable.process_icon);
		pd.setCancelable(cancelable);
		pd.show();
		return pd;
	}
	
	/**
	 * 
	 * @param separater
	 * @param tf
	 * @return
	 */
	public String getTime(String separater, TimeFormat tf) {
		if (separater == null) {
			throw new UnknownFormatFlagsException("unsupported for separater '" + separater + "'");
		}
		Calendar c = Calendar.getInstance();

		int h;
		int m;
		int s;
		h = c.get(Calendar.HOUR_OF_DAY);
		m = c.get(Calendar.MINUTE);
		s = c.get(Calendar.SECOND);
		if (tf == TimeFormat._12H) {
			String f;
			if (h >= 12) {
				f = "PM";
				h = h - 12;
			} else {
				f = "AM";
			}
			return h + separater + m + separater + s + " " + f;
		}
		return h + separater + m + separater + s;
	}

	/**
	 * 
	 * @param title
	 *            If do not use title then set value null
	 * @param msg
	 *            Message to show
	 */
	public AlertDialog showMessageDialog(String title, String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.info_icon);
		if (title == null) {
			return builder.setMessage(msg).show();
		} else {
			return builder.setMessage(msg).setTitle(title).show();
		}
	}
	
	public AlertDialog showMessageDialog(String title, String msg, String button1, String button2, OnClickListener button1Click, OnClickListener button2Click) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.info_icon);
		if (title == null) {
			builder.setMessage(msg);
		} else {
			builder.setMessage(msg).setTitle(title);
		}		
		builder.setPositiveButton(button1, button1Click);
		builder.setNeutralButton(button2, button2Click);
		return builder.show();
	}
	
	public AlertDialog showErrorDialog(String msg){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.error_icon);
		builder.setMessage(msg);
		builder.setTitle("Error!");
		builder.setPositiveButton("OK", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		return builder.show();
	}

	public AlertDialog showWarningDialog(String msg, String button1, String button2, String button3, OnClickListener eventbutton1, OnClickListener eventbutton2, OnClickListener eventbutton3) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg == null ? "" : msg);
		builder.setIcon(R.drawable.warning);		
		builder.setPositiveButton(button1, eventbutton1);
		builder.setNeutralButton(button2, eventbutton2);
		builder.setNegativeButton(button3, eventbutton3);
		builder.setTitle("Warning");
		return builder.show();
	}
	
	public AlertDialog showWarningDialog(String msg, String button1, String button2, OnClickListener eventbutton1, OnClickListener eventbutton2) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg == null ? "" : msg);
		builder.setIcon(R.drawable.warning);		
		builder.setPositiveButton(button1, eventbutton1);
		builder.setNeutralButton(button2, eventbutton2);
		builder.setTitle("Warning");
		return builder.show();
	}
	
	public AlertDialog showWarningDialog(String msg, String button1, OnClickListener eventbutton1) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg == null ? "" : msg);
		builder.setIcon(R.drawable.warning);		
		builder.setPositiveButton(button1, eventbutton1);
		builder.setTitle("Warning");
		return builder.show();
	}
	
	public AlertDialog showConfirmDialog(String title, String msg, OnClickListener yesClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.question_icon);
		if(title!=null) builder.setTitle(title);
		if(msg!=null) builder.setMessage(msg);
		return builder.setPositiveButton("Yes", yesClick).setNegativeButton("No", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		}).show();
	}

	/**
	 * Start an activity
	 * 
	 * @param nextActivity
	 *            Next Activity
	 */
	protected void startActivity(Class<? extends Activity> nextActivity) {
		Intent intent = new Intent(this, nextActivity);
		startActivity(intent);
	}

}
